package org.lanqiao.wxq.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.lanqiao.tools.BaseDao;
import org.lanqiao.tools.BaseServlet;

import com.google.gson.Gson;

@WebServlet("/CompanyListServlet")
public class CompanyListServlet extends BaseServlet {
	Gson gson= new Gson();
	
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.request = request;
		this.response = response;

		try {	
//热门公司、没有根据点击量查	
			  if ("HotCompany".equals(getString("type"))) {
			    	String sql = "select id,shortName,address,field,logId,labelId,period from company";
			    	List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler());
			    	System.out.println("HotCompany");
			    	response.getWriter().write(gson.toJson(query));
//查询公司对应的职位			    	
				} else if ("CompanyPosition".equals(getString("type"))) {
					String sql= "SELECT c.id as companyId,p.positionName FROM company c,position p where c.id=p.companyId GROUP BY c.id,p.positionName";
					List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler());
					System.out.println("CompanyPosition");
					response.getWriter().write(gson.toJson(query));
//创建session会话					
			    }else if ("setCid".equals(getString("type"))) {
					System.out.println("setCid");
					Integer cid = getInt("cid");
					HttpSession session = request.getSession();
					session.setAttribute("cid", cid);
					session.setAttribute("id", cid);
					String sql = "SELECT p.*,c.id,c.shortName,c.com,c.number,c.field,c.logId,c.period,c.sateIds from position p,company c where p.companyId=c.id and c.id=?";
					List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler(), cid);
					response.getWriter().write(gson.toJson(query));
				} else if ("getCid".equals(getString("type"))) {
					Object cid = request.getSession().getAttribute("cid");
					System.out.println(request.getSession().getId() + "111/cid=" + cid);
					String sql = "SELECT * from company where id=?";
					Map<String, Object> query = BaseDao.query.query(sql, new MapHandler(), cid);
					response.getWriter().write(gson.toJson(query));
//公司详情页-招聘职位					
				}else if ("selectPositionByCid".equals(getString("type"))) {
					Object cid = request.getSession().getAttribute("cid");
					System.out.println(request.getSession().getId() + "222/cid=" + cid);
					String sql = "SELECT p.* from position p, company c where p.companyId=c.id and c.id=? order by p.date desc limit 0,5";
					List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler(), cid);
					response.getWriter().write(gson.toJson(query));
//公司详情页-创始人
				}else if ("selectFounderByCid".equals(getString("type"))) {
					Object cid = request.getSession().getAttribute("cid");
					System.out.println(request.getSession().getId() + "333/cid=" + cid);
					String sql = "SELECT f.* from founder f,company c where f.companyId=c.id and c.id=?";
					List<Map<String, Object>> query = BaseDao.query.query(sql, new MapListHandler(), cid);
					System.out.println(query);
					response.getWriter().write(gson.toJson(query));
				}
			  
			}catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

}